package org.example.myleet.p917;

public class Solution {
    public String reverseOnlyLetters(String s) {
        char[] charArr = s.toCharArray();
        int n = charArr.length, l = 0, r = n - 1;
        char swap;
        while (l < r) {
            while (l < n && l < r && !Character.isAlphabetic(charArr[l])) {
                ++l;
            }
            while (r >= 0 && l < r && !Character.isAlphabetic(charArr[r])) {
                --r;
            }
            if (l < n && r >= 0) {
                swap = charArr[l];
                charArr[l] = charArr[r];
                charArr[r] = swap;
            }
            ++l;
            --r;
        }
        return new String(charArr);
    }
}
